package ynu.edu;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/fallback")
public class FallbackController {

    /**
     * 用户服务降级处理
     */
    @GetMapping("/user")
    public Mono<ResponseEntity<Map<String, Object>>> userFallback(ServerWebExchange exchange) {
        Map<String, Object> response = new HashMap<>();
        response.put("code", 503);
        response.put("message", "用户服务暂时不可用，已启用熔断保护");
        response.put("timestamp", LocalDateTime.now());
        response.put("service", "user-service");
        response.put("status", "CIRCUIT_BREAKER_OPEN");
        
        return Mono.just(ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(response));
    }

    /**
     * 购物车服务降级处理
     */
    @GetMapping("/cart")
    public Mono<ResponseEntity<Map<String, Object>>> cartFallback(ServerWebExchange exchange) {
        Map<String, Object> response = new HashMap<>();
        response.put("code", 503);
        response.put("message", "购物车服务暂时不可用，已启用熔断保护");
        response.put("timestamp", LocalDateTime.now());
        response.put("service", "cart-service");
        response.put("status", "CIRCUIT_BREAKER_OPEN");
        
        return Mono.just(ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(response));
    }

    /**
     * 默认降级处理
     */
    @GetMapping("/default")
    public Mono<ResponseEntity<Map<String, Object>>> defaultFallback(ServerWebExchange exchange) {
        Map<String, Object> response = new HashMap<>();
        response.put("code", 503);
        response.put("message", "服务暂时不可用，已启用熔断保护");
        response.put("timestamp", LocalDateTime.now());
        response.put("service", "unknown");
        response.put("status", "CIRCUIT_BREAKER_OPEN");
        
        return Mono.just(ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(response));
    }
} 